package com.ford.trade.hotel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ford.trade.CoreHttpUtils;
import com.ford.trade.dto.QueryDTO;
import com.ford.trade.entity.Hotel;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :  zhangfutao
 * @version :  V-1.0
 * @description :
 * @date :   2022/5/24 14:33
 */
@RestController
public class HotelController {

    @Resource
    private HotelService hotelService;

    @PostMapping("getHotel")
    public void getHotel(@RequestBody QueryDTO queryDTO) throws ParseException {
        String hotels = "昭平恒丰酒店\n";
        String[] hotelList = hotels.split("\n");
//        for (String name : hotelList) {
            String api = "https://restapi.amap.com/v3/place/text?parameters";
            Map<String, String> headerMap = new HashMap<>();
            headerMap.put("key", "ddb9c113d974c2e0c7dd246a1b4fe9ca");
            headerMap.put("keywords", "酒店");
            headerMap.put("types","100100");
            headerMap.put("city",queryDTO.getType());
            headerMap.put("page",queryDTO.getIndexId().toString());
            String url = CoreHttpUtils.concatQueryString(api, headerMap, false, null);
            try {
                String withHeaders = CoreHttpUtils.get(url, "utf-8", 2);
                System.out.println("1:"+withHeaders);
                JSONObject jsonObject = JSON.parseObject(withHeaders, JSONObject.class);
                JSONArray pois = jsonObject.getJSONArray("pois");
                for (Object o : pois) {
                    Hotel hotel = new Hotel();
                    JSONObject poisJSONObject = (JSONObject) o;
                    hotel.setHotelId(poisJSONObject.getString("id"));
                    hotel.setHotelName(poisJSONObject.getString("name"));
                    hotel.setProvince(poisJSONObject.getString("pname"));
                    hotel.setCity(poisJSONObject.getString("cityname"));
                    hotel.setTel(poisJSONObject.getString("tel"));
                    hotel.setPoi(poisJSONObject.getString("location"));
                    hotel.setCategory(poisJSONObject.getString("type"));
                    hotel.setCounty(poisJSONObject.getString("adname"));
                    hotel.setLocation(poisJSONObject.getString("address"));
                    JSONArray photos = poisJSONObject.getJSONArray("photos");
                    if (!CollectionUtils.isEmpty(photos)){
                        hotel.setUrl(photos.getJSONObject(0).getString("url"));
                    }
                    String api2 = "https://restapi.amap.com/v3/place/detail?parameters";
                    Map<String, String> headerMap2 = new HashMap<>();
                    headerMap2.put("key", "ddb9c113d974c2e0c7dd246a1b4fe9ca");
                    headerMap2.put("id", hotel.getHotelId());
                    String url2 = CoreHttpUtils.concatQueryString(api2, headerMap2, false, null);
                    String s = CoreHttpUtils.get(url2, "utf-8", 2);
                    JSONObject jsonObject2 = JSON.parseObject(s, JSONObject.class);
                    JSONArray pois2 = jsonObject2.getJSONArray("pois");
                    JSONObject poisJSONObject2 = pois2.getJSONObject(0);
                    System.out.println("s:" + s);
                    JSONObject biz_ext = poisJSONObject2.getJSONObject("biz_ext");
                    hotel.setScore(biz_ext.getString("rating"));
                    hotelService.save(hotel);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

//    }
}
